package cn.iocoder.yudao.module.wine.api;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.api.brokerage.BrokerageUserApi;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import cn.iocoder.yudao.module.wine.api.dto.BrokerageUserNewDTO;
import cn.iocoder.yudao.module.wine.api.dto.MemberDeadlineDTO;
import cn.iocoder.yudao.module.wine.api.dto.MemberOpenLogDTO;
import cn.iocoder.yudao.module.wine.api.dto.MemberTypeDTO;
import cn.iocoder.yudao.module.wine.dal.dataobject.memberdeadline.MemberDeadlineDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.memberopenlog.MemberOpenLogDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.module.wine.dal.mysql.memberopenlog.MemberOpenLogMapper;
import cn.iocoder.yudao.module.wine.service.memberdeadline.MemberDeadlineService;
import cn.iocoder.yudao.module.wine.service.membertype.MemberTypeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * @author zyc
 * @createDate 2024/5/15 14:55
 */
@Service
@Validated
public class MemberDeadlineApiImpl implements MemberDeadlineApi{

    @Resource
    private MemberDeadlineService memberDeadlineService;

    @Resource
    private BrokerageUserApi brokerageUserApi;

    @Resource
    private MemberTypeService memberTypeService;

    @Resource
    private MemberOpenLogMapper memberOpenLogMapper;

    @Override
    public MemberTypeDTO getMemberTypeInfo(Long memberTypeId) {
        MemberTypeDO memberType = memberTypeService.getMemberType(memberTypeId);
        return BeanUtils.toBean(memberType, MemberTypeDTO.class);
    }

    /**
     * 获取用户期限(有效期内的数据)
     * @param userId
     * @return
     */
    @Override
    public MemberDeadlineDTO getMemberDeadlineInfoByUserId(Long userId) {

        MemberDeadlineDO data = memberDeadlineService.getMemberDeadlineInfoByUserId(userId);
        return BeanUtils.toBean(data, MemberDeadlineDTO.class);
    }
    /**
     * 获取用户期限
     * @param userId
     * @return
     */
    @Override
    public MemberDeadlineDTO getMemberDeadlineByUserId(Long userId) {
        MemberDeadlineDO data = memberDeadlineService.getMemberDeadlineByUserId(userId);
        return BeanUtils.toBean(data, MemberDeadlineDTO.class);

    }

    @Override
    public Boolean addMemberDeadline(MemberDeadlineDTO memberDeadlineDTO) {
        return memberDeadlineService.addMemberDeadline(memberDeadlineDTO);
    }

    @Override
    public Boolean editMemberDeadline(MemberDeadlineDTO memberDeadlineDTO) {
        return memberDeadlineService.editMemberDeadline(memberDeadlineDTO);
    }

    @Override
    public Boolean addMemberOpenLog(MemberOpenLogDTO logDTO) {
        MemberOpenLogDO bean = BeanUtils.toBean(logDTO, MemberOpenLogDO.class);
        return memberOpenLogMapper.insert(bean) > 0;

    }

    @Override
    public Boolean editMemberOpenLog(MemberOpenLogDTO logDTO) {
        MemberOpenLogDO bean = BeanUtils.toBean(logDTO, MemberOpenLogDO.class);
        return memberOpenLogMapper.updateById(bean) > 0;

    }

    @Override
    public MemberOpenLogDTO getMemberOpenLog(String outTradeNo) {
        MemberOpenLogDO memberOpenLogDO = memberOpenLogMapper.selectOne(new LambdaQueryWrapperX<MemberOpenLogDO>().eq(MemberOpenLogDO::getTradeNo, outTradeNo));
        return BeanUtils.toBean(memberOpenLogDO, MemberOpenLogDTO.class);
    }




    @Override
    public BrokerageUserNewDTO getBrokerageUserByIds(Long bindUserId, Long loginUserId) {
        BrokerageUserDTO infoByUserId = brokerageUserApi.getInfoByUserIds(bindUserId,loginUserId);
        return BeanUtils.toBean(infoByUserId,BrokerageUserNewDTO.class);
    }

    @Override
    public BrokerageUserNewDTO getBindUserId(Long userId) {
        BrokerageUserDTO bindUserId = brokerageUserApi.getBindUserId(userId);
        return BeanUtils.toBean(bindUserId, BrokerageUserNewDTO.class);
    }


    @Override
    public BrokerageUserNewDTO getBrokerageUser(Long userId) {
        BrokerageUserDTO infoByUserId = brokerageUserApi.getInfoByUserId(userId);
        return BeanUtils.toBean(infoByUserId,BrokerageUserNewDTO.class);
    }

    @Override
    public BrokerageUserNewDTO addBrokerageUser(BrokerageUserNewDTO data) {

        BrokerageUserDTO bean = BeanUtils.toBean(data, BrokerageUserDTO.class);
        brokerageUserApi.addBrokerageUser(bean);

        return BeanUtils.toBean(brokerageUserApi.getInfoByUserId(data.getBindUserId()),BrokerageUserNewDTO.class);
    }

    @Override
    public Boolean updateBrokerageUser(BrokerageUserNewDTO data) {
        BrokerageUserDTO bean = BeanUtils.toBean(data, BrokerageUserDTO.class);
        return brokerageUserApi.updateBrokerageUser(bean);
    }

}
